package baidu.com.matedemo.dlp;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.baidu.duer.smartmate.protocol.dlp.bean.autentication.DeviceCodePairReturnPayload;
import com.baidu.duer.smartmate.proxy.bean.AuthenticationMessage;
import com.baidu.duer.smartmate.proxy.controller.AuthenticationObserver;
import com.baidu.duer.smartmate.proxy.controller.ControllerManager;

import baidu.com.matedemo.base.BaseFragment;
import baidu.com.matedemo.R;
import baidu.com.matedemo.DemoApp;
import baidu.com.matedemo.util.DemoUtils;

/**
 * Created by niufei01 on 2017/9/13.
 */

public class AuthenticationDemo extends BaseFragment implements AuthenticationObserver {

    ControllerManager controllerManager;
    ViewHolder viewHolder = null;

    @Override
    public View onCreateContentViewCompat(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.authentication, viewGroup, false);
    }

    @Override
    public void onContentViewCreated(View view) {
        controllerManager = DemoApp.getInstance().getCurreDuerDevice().getControllerManager();
        controllerManager.registerAuthenticationObserver(this);
        viewHolder = new ViewHolder(view);
        viewHolder.btnVerify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 连接成功后 开发者可以根据verifyUser()鉴权 ，验证当前连接的设备登录账号是否与app相同
                // 合作方请直接调用
                DemoApp.getInstance().getCurreDuerDevice().verifyUser();
            }
        });
        viewHolder.btnBduss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controllerManager.passportPair();
            }
        });
        viewHolder.btnThirdKey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                controllerManager.thirdPartyPair("Your third login key");
            }
        });
    }

    @Override
    public void onDataChanaged(String event, AuthenticationMessage message) {
        viewHolder.edittext.setText(DemoUtils.toFormatJson(message));
    }

    @Override
    public void onDeviceCodePairReturn(DeviceCodePairReturnPayload payload) {
    }

    public class ViewHolder {
        public EditText edittext;
        public Button getstatus;
        public Button btnBduss;
        public Button btnVerify;
        public Button btnThirdKey;
        public LinearLayout buttonLayout;

        public ViewHolder(View rootView) {
            this.edittext = (EditText) rootView.findViewById(R.id.edittext);
            this.getstatus = (Button) rootView.findViewById(R.id.getstatus);
            this.getstatus.setVisibility(View.GONE);
            this.btnVerify = (Button) rootView.findViewById(R.id.btn_verify);
            this.btnBduss = (Button) rootView.findViewById(R.id.btn_bduss);
            this.buttonLayout = (LinearLayout) rootView.findViewById(R.id.button_layout);
            this.btnThirdKey = (Button) rootView.findViewById(R.id.btn_thirdkey);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (controllerManager != null) {
            controllerManager.unregisterAuthenticationObserver(this);
        }
    }
}
